package com.higgins.eventtracking.client.health.fundamentals;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HTMLTable.Cell;
import com.google.gwt.user.client.ui.Label;
import com.higgins.eventtracking.client.EventTrackingService;
import com.higgins.eventtracking.client.EventTrackingServiceAsync;
import com.higgins.eventtracking.client.dto.GoalDTO;
import com.higgins.eventtracking.client.dto.HealthAlertDTO;
import com.higgins.eventtracking.client.dto.TimeTableTaskDTO;
import com.higgins.eventtracking.client.genericcomponents.GenericColumn;
import com.higgins.eventtracking.client.genericcomponents.GenericColumns;
import com.higgins.eventtracking.client.genericcomponents.GenericTable;

public class GoalsTable extends GenericTable	{
	private static final Logger log = Logger.getLogger(GoalsTable.class.getName());
	private final EventTrackingServiceAsync greetingService = GWT
			.create(EventTrackingService.class);
	private String frameOfMind = null;
	
	public GoalsTable(GenericColumns genericColumns) {
		super(genericColumns);
		listsLoaded();
	}
	public void listsLoaded()	{
		retrieveDTOs();
	}
	public void setFrameOfMind(String frameOfMind)	{
		this.frameOfMind = frameOfMind;
		refresh();
	}
	public void retrieveDTOs()	{
		greetingService.retrieveGoals(frameOfMind, new AsyncCallback<List<GoalDTO>>()  {
			@Override
			public void onFailure(Throwable caught) {
				log.info("TimeTableTable: " + caught.toString());
			}

			@Override
			public void onSuccess(List<GoalDTO> result) {
				loadData(convertDTOtoRowArray(result));
			}
		});		
	}
	private List<List<Object>> convertDTOtoRowArray(List<GoalDTO> dtos)	{
		List<List<Object>> rows = new ArrayList<List<Object>>();
		for (GoalDTO dto:dtos)	{
			List<Object> row = new ArrayList<Object>();
			row.add(dto.getId());
			row.add(dto.getTask()!=null?dto.getTask():"");
			row.add(dto.getIntensity()!=null?dto.getIntensity():0);
			row.add(null);
			row.add(null);
			rows.add(row);
		}
		return rows;
	}
	protected void addObject(int row, int column, Object object)	{
		ensureRowAvailable(row);
		GenericColumn genericColumn = genericColumns.getColumn(column);
//		if (genericColumn.getType().equals(JobSiteDTO.class))	{
//			addJobSiteDTO(row, column, object, genericColumn);
		if (column==3)	{
			addLabel(row, column, object, genericColumn);
		}
		if (column==4)	{
			addButton(row, column, object, genericColumn);
		}
	}
	
	public void addLabel(int row, int column, Object object, GenericColumn genericColumn)	{
		Label newLabel = new Label("Up");
		setWidget(row, column, newLabel);
		getCellFormatter().addStyleName(row, column, "FlexTable-normCell");
	}
	
	public void addButton(int row, int column, Object object, GenericColumn genericColumn)	{
		Button newButton = new Button("Complete");
		setWidget(row, column, newButton);
		getCellFormatter().addStyleName(row, column, "FlexTable-normCell");
		newButton.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				Cell cell = getCellForEvent(event);
				final int row = cell.getRowIndex();
				completeGoal(row);
			}});
	}
	
	public void completeGoal(int row)	{
		GoalDTO dto = new GoalDTO();
		dto.setId(getId(row));
		dto.setTask(getText(row, 1));
		dto.setIntensity(getInteger(row, 2));
		dto.setFrameOfMind(frameOfMind);
		dto.setOrder(row);
		completeGoal(dto);
	}
	
	public void updateItemCell(int row)	{
		updateItemCellPriority(row, 0);
	}

	public void updateItemCellPriority(int row, int move)	{
		GoalDTO dto = new GoalDTO();
		dto.setId(getId(row));
		dto.setTask(getText(row, 1));
		dto.setIntensity(getInteger(row, 2));
		dto.setFrameOfMind(frameOfMind);
		dto.setOrder(row + move);
		updateRow(dto);
	}

	private void completeGoal(GoalDTO dto)	{
		try {
			greetingService.completeGoal(dto, new AsyncCallback<String>()	{
				@Override
				public void onFailure(Throwable caught) {
					log.info("HealthAlertTable: " + caught.toString());
				}

				@Override
				public void onSuccess(String result) {
					refresh();					
				}
			});
		} catch (Exception e)	{
			log.info("GoalsTable: " + e.toString());
		}
	}
	
	private void updateRow(GoalDTO dto)	{
		try {
			greetingService.updateGoal(dto, new AsyncCallback<String>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("HealthAlertTable: " + caught.toString());
					}

					@Override
					public void onSuccess(String result) {
						setRowIdAfterUpdate(result);
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("HealthAlertTable: " + e.toString());
		} /*catch (SecurityAccessDeniedException e) {
			log.info("SecurityAccessDeniedException: " + e.toString());
		} 		*/
	}
	protected void deleteRow(Long id)	{
		try	{
			greetingService.deleteGoal(id, new AsyncCallback<Long>()   {
				@Override
				public void onFailure(Throwable caught) {
					log.info("GoalTable: Delete Goal Failed!");
				}

				@Override
				public void onSuccess(Long id) {
					refresh();
				}  
			});
		} catch (Exception e)	{
			log.info("GoalTable: " + e.toString());
		}
	}
	protected void makeCellEditable(GenericColumn genericColumn, final int row, final int column)	{
		if (column==3 && row > 1)	{
			updateItemCellPriority(row, -1);
			updateItemCellPriority(row-1, +1);
			refresh();
		}
	}
	
}
